package com.example.ercmssystem2.controller;

import com.example.ercmssystem2.entity.Resource;
import com.example.ercmssystem2.entity.ResourceType;
import com.example.ercmssystem2.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/resources")
@CrossOrigin(origins = "*")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    // 创建资源
    @PostMapping
    public ResponseEntity<?> createResource(@RequestBody Resource resource, @RequestParam Long createdBy) {
        try {
            Resource created = resourceService.createResource(resource, createdBy);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "资源创建成功");
            response.put("data", created);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 更新资源
    @PutMapping("/{id}")
    public ResponseEntity<?> updateResource(@PathVariable Long id, @RequestBody Resource resource) {
        try {
            Resource updated = resourceService.updateResource(id, resource);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "资源更新成功");
            response.put("data", updated);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 删除资源
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteResource(@PathVariable Long id) {
        try {
            resourceService.deleteResource(id);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "资源删除成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取资源详情
    @GetMapping("/{id}")
    public ResponseEntity<?> getResource(@PathVariable Long id) {
        try {
            Resource resource = resourceService.getResourceById(id);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", resource);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取所有资源
    @GetMapping
    public ResponseEntity<?> getAllResources() {
        try {
            List<Resource> resources = resourceService.getAllResources();
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", resources);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 按部门获取资源
    @GetMapping("/department/{departmentId}")
    public ResponseEntity<?> getResourcesByDepartment(@PathVariable Long departmentId) {
        try {
            List<Resource> resources = resourceService.getResourcesByDepartment(departmentId);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", resources);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 分配资源到部门
    @PutMapping("/{resourceId}/assign-department")
    public ResponseEntity<?> assignResourceToDepartment(@PathVariable Long resourceId, @RequestParam Long departmentId) {
        try {
            Resource resource = resourceService.assignResourceToDepartment(resourceId, departmentId);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "资源分配到部门成功");
            response.put("data", resource);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取部门可用资源
    @GetMapping("/department/{departmentId}/available")
    public ResponseEntity<?> getAvailableResourcesByDepartment(@PathVariable Long departmentId) {
        try {
            List<Resource> resources = resourceService.getAvailableResourcesByDepartment(departmentId);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", resources);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取资源类型列表
    @GetMapping("/types")
    public ResponseEntity<?> getAllResourceTypes() {
        try {
            List<ResourceType> types = resourceService.getAllResourceTypes();
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", types);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 新建资源类型
    @PostMapping("/types")
    public ResponseEntity<?> createResourceType(@RequestBody ResourceType resourceType) {
        try {
            ResourceType created = resourceService.createResourceType(resourceType);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "资源类型创建成功");
            response.put("data", created);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
} 